/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import tpa.TablaPeriodica.Elemento;
import tpa.TablaPeriodica.SubClase;

/**
 *
 * @author leandro
 */
public class AgregarSubClaseInit extends TablaPeriodicaBaseAction
{

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response)
            throws Exception
    {
        try
        {
            AgregarSubClaseActionForm actionForm = (AgregarSubClaseActionForm) form;

            if (actionForm.getSubClaseId() > 0L)
            {
                SubClase subclase = TablaPeriodicaBusinessDelegate.getInstance().getTablaPeriodicaSessionBean().ObtenerSubClase(actionForm.getSubClaseId());
                actionForm.setClasificacionId(subclase.getParentClasificacion().getId());

                if (subclase.getElementos().size() > 0)
                {
                    actionForm.setElementosElegidos(new Long[subclase.getElementos().size()]);
                    for (Elemento currentElemento : subclase.getElementos())
                    {
                        actionForm.getElementosElegidos()[subclase.getElementos().indexOf(currentElemento)] = currentElemento.getId();
                    }
                }

                actionForm.setValor(subclase.getValor());
            }
            else
            {
                actionForm.setClasificacionId(0L);
                actionForm.setStatus("");
                actionForm.setValor("");
                actionForm.setElementosElegidos(new Long[0]);
            }

            actionForm.setClasificaciones(TablaPeriodicaBusinessDelegate.getInstance().getTablaPeriodicaSessionBean().ObtenerClasificaciones());
            actionForm.setElementos(TablaPeriodicaBusinessDelegate.getInstance().getTablaPeriodicaSessionBean().ObtenerElementos());

            return mapping.findForward(SUCCESS);
        }
        catch (Exception exception)
        {
            ActionErrors errors = new ActionErrors();

            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("error.exception", exception.getMessage()));

            saveErrors(request, errors);
            return mapping.findForward(ERROR);
        }
    }
}
